/*
 *     Copyright 2010 Jean-Paul Balabanian and Yngve Devik Hammersland
 *
 *     This file is part of glsl4idea.
 *
 *     Glsl4idea is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as
 *     published by the Free Software Foundation, either version 3 of
 *     the License, or (at your option) any later version.
 *
 *     Glsl4idea is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with glsl4idea.  If not, see <http://www.gnu.org/licenses/>.
 */

package glslplugin;

import com.intellij.lexer.Lexer;
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.fileTypes.SyntaxHighlighterBase;
import com.intellij.psi.tree.IElementType;
import glslplugin.lang.scanner.GLSLFlexAdapter;
import static glslplugin.lang.elements.GLSLTokenTypes.*;
import org.jetbrains.annotations.NotNull;

import java.awt.*;

public class GLSLHighlighter extends SyntaxHighlighterBase {
    static final TextAttributesKey GLSL_NUMBER = TextAttributesKey.createTextAttributesKey("GLSL.NUMBER", DefaultLanguageHighlighterColors.NUMBER);
    static final TextAttributesKey GLSL_TYPE_SPECIFIER = TextAttributesKey.createTextAttributesKey("GLSL.TYPE_SPECIFIER", DefaultLanguageHighlighterColors.KEYWORD);
    static final TextAttributesKey GLSL_TYPE_QUALIFIERS = TextAttributesKey.createTextAttributesKey("GLSL.QUALIFIER_TOKENS", DefaultLanguageHighlighterColors.METADATA);
    static final TextAttributesKey GLSL_PARAMETER_QUALIFIERS = TextAttributesKey.createTextAttributesKey("GLSL.PARAMETER_QUALIFIERS", DefaultLanguageHighlighterColors.METADATA);
    static final TextAttributesKey GLSL_FLOW_KEYWORDS = TextAttributesKey.createTextAttributesKey("GLSL.FLOW_KEYWORDS", DefaultLanguageHighlighterColors.KEYWORD);
    static final TextAttributesKey GLSL_LINE_COMMENT = TextAttributesKey.createTextAttributesKey("GLSL.LINE_COMMENT", DefaultLanguageHighlighterColors.LINE_COMMENT);
    static final TextAttributesKey GLSL_BLOCK_COMMENT = TextAttributesKey.createTextAttributesKey("GLSL.BLOCK_COMMENT", DefaultLanguageHighlighterColors.BLOCK_COMMENT);
    static final TextAttributesKey GLSL_IDENTIFIER = TextAttributesKey.createTextAttributesKey("GLSL.IDENTIFIER", DefaultLanguageHighlighterColors.IDENTIFIER);
    static final TextAttributesKey GLSL_COMPILER_DIRECTIVE_VERSION = TextAttributesKey.createTextAttributesKey("GLSL.COMPILER_DIRECTIVE_VERSION", DefaultLanguageHighlighterColors.DOC_COMMENT_TAG_VALUE);
    static final TextAttributesKey GLSL_COMPILER_DIRECTIVE_EXTENSION = TextAttributesKey.createTextAttributesKey("GLSL.COMPILER_DIRECTIVE_EXTENSION", DefaultLanguageHighlighterColors.DOC_COMMENT_TAG);
    static final TextAttributesKey GLSL_COMPILER_DIRECTIVE_PRAGMA = TextAttributesKey.createTextAttributesKey("GLSL.COMPILER_DIRECTIVE_PRAGMA", DefaultLanguageHighlighterColors.DOC_COMMENT);
    static final TextAttributesKey GLSL_COMPILER_DIRECTIVE_OTHER = TextAttributesKey.createTextAttributesKey("GLSL.COMPILER_DIRECTIVE_OTHER", DefaultLanguageHighlighterColors.DOC_COMMENT_MARKUP);
    static final TextAttributesKey GLSL_SEMICOLON = TextAttributesKey.createTextAttributesKey("GLSL.GLSL_SEMICOLON", DefaultLanguageHighlighterColors.SEMICOLON);
    static final TextAttributesKey GLSL_COMMA = TextAttributesKey.createTextAttributesKey("GLSL.COMMA", DefaultLanguageHighlighterColors.COMMA);
    static final TextAttributesKey GLSL_BRACES = TextAttributesKey.createTextAttributesKey("GLSL.BRACES", DefaultLanguageHighlighterColors.BRACES);
    static final TextAttributesKey GLSL_BRACKETS = TextAttributesKey.createTextAttributesKey("GLSL.BRACKETS", DefaultLanguageHighlighterColors.BRACKETS);
    static final TextAttributesKey GLSL_PARENTHESES = TextAttributesKey.createTextAttributesKey("GLSL.PARENTHESES", DefaultLanguageHighlighterColors.PARENTHESES);
    static final TextAttributesKey GLSL_TEXT = TextAttributesKey.createTextAttributesKey("GLSL.MISC", DefaultLanguageHighlighterColors.IDENTIFIER);
    static final TextAttributesKey GLSL_UNKNOWN = TextAttributesKey.createTextAttributesKey("GLSL.UNKNOWN", new TextAttributes(new Color(0xFF6B68),null,null,null,Font.BOLD));

    public GLSLHighlighter() {
        super();
    }


    @NotNull
    public Lexer getHighlightingLexer() {
        return new GLSLFlexAdapter();
    }

    @NotNull
    public TextAttributesKey[] getTokenHighlights(IElementType type)
    {
        if (type == INTEGER_CONSTANT || type == FLOAT_CONSTANT || type == BOOL_CONSTANT) {
            return new TextAttributesKey[]{GLSL_NUMBER};
        } else if (type == COMMENT_BLOCK) {
            return new TextAttributesKey[]{GLSL_BLOCK_COMMENT};
        } else if (type == COMMENT_LINE) {
            return new TextAttributesKey[]{GLSL_LINE_COMMENT};
        } else if (type == IDENTIFIER) {
            return new TextAttributesKey[]{GLSL_IDENTIFIER};
        } else if (type == COMPILER_DIRECTIVE_VERSION) {
            return new TextAttributesKey[]{GLSL_COMPILER_DIRECTIVE_VERSION};
        } else if (type == COMPILER_DIRECTIVE_EXTENSION) {
            return new TextAttributesKey[]{GLSL_COMPILER_DIRECTIVE_EXTENSION};
        } else if (type == COMPILER_DIRECTIVE_PRAGMA) {
            return new TextAttributesKey[]{GLSL_COMPILER_DIRECTIVE_PRAGMA};
        } else if (type == COMPILER_DIRECTIVE_OTHER) {
            return new TextAttributesKey[]{GLSL_COMPILER_DIRECTIVE_OTHER};
        } else if (TYPE_SPECIFIER_NONARRAY_TOKENS.contains(type)) {
            return new TextAttributesKey[]{GLSL_TYPE_SPECIFIER};
        } else if (QUALIFIER_TOKENS.contains(type)) {
            return new TextAttributesKey[]{GLSL_TYPE_QUALIFIERS};
        } else if (PARAMETER_QUALIFIERS.contains(type)) {
            return new TextAttributesKey[]{GLSL_PARAMETER_QUALIFIERS};
        } else if (FLOW_KEYWORDS.contains(type)) {
            return new TextAttributesKey[]{GLSL_FLOW_KEYWORDS};
        } else if (type == SEMICOLON ) {
            return new TextAttributesKey[]{GLSL_SEMICOLON};
        } else if (type == COMMA ) {
            return new TextAttributesKey[]{GLSL_COMMA};
        } else if (type == LEFT_BRACE || type == RIGHT_BRACE ) {
            return new TextAttributesKey[]{GLSL_BRACES};
        } else if (type == LEFT_BRACKET || type == RIGHT_BRACKET ) {
            return new TextAttributesKey[]{GLSL_BRACKETS};
        } else if (type == LEFT_PAREN || type == RIGHT_PAREN ) {
            return new TextAttributesKey[]{GLSL_PARENTHESES};
        } else if (type == UNKNOWN) {
            return new TextAttributesKey[]{GLSL_UNKNOWN};
        }
        return new TextAttributesKey[]{TextAttributesKey.createTextAttributesKey("GLSL.TEXT", GLSL_TEXT)};
    }
}
